﻿using System;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;

using Mystery.SharePoint;

namespace Mystery.SharePoint.Layouts.SPM
{
    public partial class QuotaAdmin : LayoutsPageBase
    {
        private const double bytesPerMB = 1048576;

        public long StorageMaximumLevel { get; set; }
        public long StorageWarningLevel { get; set; }
        public double UserCodeMaximumLevel { get; set; }
        public double UserCodeWarningLevel { get; set; }
        public long DiscussionStorage { get; set; }
        public long Storage { get; set; }
       
        public double StorageMaximumLevelMB { get { return this.StorageMaximumLevel / bytesPerMB; } }
        public double StorageWarningLevelMB { get { return this.StorageWarningLevel / bytesPerMB; } }
        public double StorageMB { get { return this.Storage / bytesPerMB; } }

        protected override void OnLoad(EventArgs e)
        {
            // since this page may run as the owner, need to elevate in order to read data
            SPSecurity.RunWithElevatedPrivileges(() =>
                {
                    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                    {
                        this.StorageMaximumLevel = site.Quota.StorageMaximumLevel;
                        this.StorageWarningLevel = site.Quota.StorageWarningLevel;
                        this.UserCodeMaximumLevel = site.Quota.UserCodeMaximumLevel;
                        this.UserCodeWarningLevel = site.Quota.UserCodeWarningLevel;                       
                        this.DiscussionStorage = site.Usage.DiscussionStorage;                      
                        this.Storage = site.Usage.Storage;                       
                    }
                });

            SPWebApplication webApp = SPContext.Current.Site.WebApplication;

            StorageRequestLink.NavigateUrl =
                string.Format(null, "mailto:{0}?subject={1}&body={2}",
                (webApp.OutboundMailReplyToAddress == null) ? "none" : webApp.OutboundMailReplyToAddress,
                string.Format(null,Resources.Page_QuotaAdmin_StorageRequest_Subject,SPContext.Current.Site.Url),
                Resources.Page_QuotaAdmin_StorageRequest_Body);
        }
    }
}
